"""
线损模块的数据模型
"""
from datetime import date, datetime
from decimal import Decimal
from typing import Optional, List

from pydantic import BaseModel, Field, ConfigDict


# ---------- RTU线损列表请求参数 ----------
class RtuLineLossListRequest(BaseModel):
	"""RTU线损列表请求参数"""
	start_date: Optional[datetime] = Field(None, description="开始日期")
	end_date: Optional[datetime] = Field(None, description="结束日期")
	serial_no: Optional[str] = Field(None, description="RTU序列号", max_length=64)
	station_id: Optional[int] = Field(None, description="台区ID")

	model_config = ConfigDict(from_attributes=True)


# ---------- RTU线损列表响应数据 ----------
class RtuLineLossItem(BaseModel):
	"""RTU线损列表项"""
	rtu_id: int = Field(..., description="RTU设备ID")
	serial_no: Optional[str] = Field(None, description="RTU序列号")
	box_serial_no: Optional[str] = Field(None, description="电箱序列号")
	station_serial_no: Optional[str] = Field(None, description="台区序列号")
	loss_rate: Decimal = Field(..., description="线损率(%)")
	loss_kwh: Decimal = Field(..., description="线损电量(kWh)")
	rtu_consumption: Decimal = Field(..., description="RTU供电量(kWh)")
	meters_consumption: Decimal = Field(..., description="电表总用电量(kWh)")
	meter_count: int = Field(..., description="电表数量")
	valid_meter_count: int = Field(..., description="有效电表数量")
	abnormal_flag: int = Field(0, description="异常标记")
	trend: Optional[str] = Field(None, description="趋势")
	calc_date: date = Field(..., description="计算日期")

	model_config = ConfigDict(from_attributes=True)


class RtuLineLossListResponse(BaseModel):
	"""RTU线损列表响应数据"""
	total: int = Field(..., description="总记录数")
	items: List[RtuLineLossItem] = Field([], description="线损列表数据")
	page: int = Field(..., description="当前页码")
	size: int = Field(..., description="每页大小")
	pages: int = Field(..., description="总页数")
	has_next: bool = Field(False, description="是否有下一页")
	has_prev: bool = Field(False, description="是否有上一页")

	model_config = ConfigDict(from_attributes=True)


# ---------- RTU线损详情响应数据 ----------
class MeterConsumptionItem(BaseModel):
	"""电表用电量明细项"""
	meter_id: int = Field(..., description="电表ID")
	serial_no: Optional[str] = Field(None, description="电表序列号")
	daily_consumption: Decimal = Field(0.0, description="日用电量(kWh)")
	has_data: bool = Field(True, description="是否有数据")

	model_config = ConfigDict(from_attributes=True)


class RtuLineLossDetailResponse(BaseModel):
	"""RTU线损详情响应数据"""
	rtu_id: int = Field(..., description="RTU设备ID")
	serial_no: Optional[str] = Field(None, description="RTU序列号")
	calc_date: date = Field(..., description="计算日期")
	loss_rate: Decimal = Field(..., description="线损率(%)")
	loss_kwh: Decimal = Field(..., description="线损电量(kWh)")
	rtu_consumption: Decimal = Field(..., description="RTU供电量(kWh)")
	meters_consumption: Decimal = Field(..., description="电表总用电量(kWh)")
	meter_count: int = Field(..., description="电表数量")
	valid_meter_count: int = Field(..., description="有效电表数量")
	meters: List[MeterConsumptionItem] = Field([], description="电表用电量明细")

	model_config = ConfigDict(from_attributes=True)


# ---------- 大屏线损概览响应数据 ----------
class LineLossOverviewResponse(BaseModel):
	"""线损概览响应数据"""
	total_loss_rate: Decimal = Field(0.0, description="总线损率(%)")
	total_loss_kwh: Decimal = Field(0.0, description="总损耗电量(kWh)")

	model_config = ConfigDict(from_attributes=True)
